$.extend({
    isotime : (timestring) ->
      date = new Date(timestring * 1000)
      hour = date.getHours()
      minute = date.getMinutes()
      hour = "0" + hour  if hour < 9
      minute = "0" + minute  if minute < 9
      result = [date.getMonth() + 1, date.getDate()]
      now = new Date()
      full_year = date.getFullYear()
      result.unshift full_year  unless now.getFullYear() is full_year
      result.join("-") + " " + [hour, minute].join(":")
    escape : (text)->
        if text
            return text.replace(/[\"&'<>]/g, (a) ->
                return {
                    '"': '&quot;'
                    '&': '&amp;'
                    "'": '&#39;'
                    '<': '&lt;'
                    '>': '&gt;'
                }[a]
            )
        return ""
    logined: (func)->
        return (args)->
            if $.user_id
                return func.apply @,args
            else
                $$ "auth.login"
    txt2html : (txt)->
        r = []
        for i in txt.replace(/\r\n/g, "\n").replace(/\r/g, "\n").split("\n")
            r.push($.escape i)
        return "<p>" + (r.join("</p><p>")) + "</p>"

    html : ->
        r = []
        _ = (o) -> r.push o
        _.html = -> r.join ''
        _

    bk : (elem, cls_func)->
        render = (_elem)->
            r = []
            _ = (o) -> r.push o
            _.render = (func)->
                if r.length
                    _elem.addClass('bked')
                    _elem.html r.join ''
                func?(_elem)
            _

        for cls_name, func of cls_func
            bk = elem.find(".bk."+cls_name)
            func(render(bk))

})

#{
#    xxx322:(render)->
#        Py.code.xxx ->
#            render(_.html()) (elem)->
#                elem.find('xxx').html xxx
#}
